﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace PIM_ATE.rflight
{
    public partial class FrmDeviceDebug : Form
    {
        ClsTcp m_tcp; string rn = "\r\n";
        String SCMD_JC = ""; String SCMD_RF = "";

        public FrmDeviceDebug()
        {
            InitializeComponent();
            SCMD_JC = "CMD" + rn +
                    "set:sweep 0" + rn +
                    "set:order 3" + rn +
                    "set:type 0  //set:port 0" + rn +
                    "set:unit 0 //dbc" + rn +
                    "set:power 43" + rn +
                    "set:F1S 1805" + rn +
                    "set:F1E 1825" + rn +
                    "set:F2S 1838" + rn +
                    "set:F2E 1878" + rn +
                    "set:step1 5" + rn +
                    "set:step2 5" + rn +
                    "set:times 5";

            SCMD_RF = "CMD扫频" + rn +
                    "SYST:FREQ:BAND 0" + rn +
                    "SYST:MEAS:MODE REFL" + rn +
                    "FREQ:MODE SWE" + rn +
                    "CARR:FREQ 1805.0,1880.0" + rn +
                    "SYST:PIM:ORD 3" + rn +
                    "SWE:STEP 1.0" + rn +
                    "CARR:POW 43.00" + rn +
                    "INIT:IMM" + rn +
                    "*OPC?" + rn +
                    "SWE:UP:CARR:FREQ?" + rn +
                    "SWE:UP:PIM:TRAC?" + rn +
                    "SWE:DN:CARR:FREQ?" + rn +
                    "SWE:DN:PIM:TRAC?" + rn +
                    "点频" + rn +
                    "SYST:FREQ:BAND 0" + rn +
                    "SYST:MEAS:MODE REFL" + rn +
                    "FREQ:MODE FIX" + rn +
                    "CARR:FREQ 1805.0,1880.0" + rn +
                    "SYST:PIM:ORD 3" + rn +
                    "CARR:POW 43.00" + rn +
                    "SYST:MEAS:TIME 5" + rn +
                    "INIT:CONT ON" + rn +
                    "INIT:IMM" + rn +
                    "*OPC?" + rn +
                    "FIX:PIM:DATA?";
        }

        private void btn_Connect_Click(object sender, EventArgs e)
        {
            if (m_tcp.connect_server(txt_Address.Text.Trim(), int.Parse(txt_Port.Text.Trim())))
            {
                txt_Mes.AppendText("connect success" + rn);
            }
            else
            {
                txt_Mes.AppendText("connect fail" + rn);
            }
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            if (m_tcp.close_server(null, null))
            {
                txt_Mes.AppendText("clsoe success" + rn);
            }
            else
            {
                txt_Mes.AppendText("clsoe fail" + rn);
            }
        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            if (m_tcp.write_exec(radioButton1.Checked))
            {
                txt_Mes.AppendText("EXEC:START" + rn);
            }
        }

        private void btn_Stop_Click(object sender, EventArgs e)
        {
            if (m_tcp.stop_test(radioButton1.Checked))
            {
                txt_Mes.AppendText("EXEC:STOP" + rn);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string readmsg = "";
            if (m_tcp.setSeting(txt_CMD.Text.Trim(), out readmsg,radioButton1.Checked))
            {
                txt_Mes.AppendText(txt_CMD.Text.Trim() + rn + readmsg + rn);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txt_Mes.AppendText(m_tcp.read() + rn);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            txt_Mes.Text = "";
        }

        private void FrmDeviceDebug_Load(object sender, EventArgs e)
        {
            m_tcp = new ClsTcp();
        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            textBox1.Text = SCMD_JC;
        }

        private void radioButton2_Click(object sender, EventArgs e)
        {
            textBox1.Text = SCMD_RF;
        }


    }
    class ClsTcp
    {
        TcpClient TCP;
        const int TIMEOUT = 100;
        public bool connect_server(string address, int port = 6307)
        {
            try
            {
                TCP = new TcpClient();
                TCP.Connect(IPAddress.Parse(address), port);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        public bool close_server(object obj, EventArgs e)
        {
            try
            {
                if (TCP.Connected)
                {
                    TCP.GetStream().Close();
                }
                TCP.Close();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        public bool write_exec(bool isJC)
        {
            string cmd = "EXEC:START\r\n";
            if (!isJC)
                cmd = "INIT:IMM\n";
            try
            {
                //begin write
                byte[] b = Encoding.ASCII.GetBytes(cmd);
                TCP.GetStream().Write(b, 0, b.Length);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        public bool stop_test(bool isJC)
        {
            string cmd = "EXEC:STOP\r\n";
            if (!isJC)
                cmd = "OUTP:STAT OFF\n";
            try
            {
                //begin write
                byte[] b = Encoding.ASCII.GetBytes(cmd);
                TCP.GetStream().Write(b, 0, b.Length);//发送数据
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        public bool setSeting(string cmd, out string readmsg,bool isJC)
        {
            string rn = "\r\n"; readmsg = "NULL";
            if (!isJC) rn = "\n";
            try
            {
                byte[] b = Encoding.ASCII.GetBytes(cmd + rn);
                TCP.GetStream().Write(b, 0, b.Length);//发送数据
                readmsg = read();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        public string read()
        {
            byte[] read = new byte[10240];
            int count = 0;
            for (int i = 0; i < TIMEOUT; i++)
            {
                if (TCP.Available > 0)
                {
                    try
                    {
                        count = TCP.GetStream().Read(read, 0, read.Length);
                    }
                    catch (Exception ex)
                    {
                        return "readMsg: net error" + ex.Message;
                    }
                    break;
                }
                if (i == TIMEOUT - 1)
                {
                    return "readMsg: timeout";
                }
                Thread.Sleep(100);
            }
            string ret = Encoding.ASCII.GetString(read, 0, count);
            return ret;
        }
    }
}
